package com.ma.crawl_book.controller;

import com.ma.crawl_book.model.SectionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

@Component
public class CrawlBookContent implements PageProcessor {

    private Site site = Site.me().setRetryTimes(5).setSleepTime(500);

    private static SectionBean sectionBean = new SectionBean();

    @Autowired
    SectionController controller;

    @Override
    public void process(Page page) {
        // 四级页面（书籍阅读页）
        if (page.getUrl().regex("http://www.23us.so/files/article/html/[0-9]*/[0-9]*/[0-9]*.html").match()) {
            String bookName = page.getHtml().xpath("//*[@id='amain']/dl/dt/a[3]/text()").get();
            // 章节名称
            String title = page.getHtml().xpath("//*[@id='amain']/dl/dd[1]/h1/text()").get();
            // 章节内容
            String content = page.getHtml().xpath("//*[@id='contents']/text()").get();
//            sectionBean.setName(title);
//            sectionBean.setBookName(bookName);
//            sectionBean.setContent(content);

        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public void startCrawl(int position, String sectionURL, int bookId) {

        Spider.create(new CrawlBookContent()).addUrl(sectionURL).thread(10).run();
//        sectionBean.setBookId(bookId);
//        sectionBean.setPosition(position);
//        controller.insertSection(sectionBean);
    }
}
